/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import com.trentech.billcalc.FinanceManager;
import com.trentech.billcalc.Resident;

/**
 * LeaveEvent is an Event for when a resident leaves the apartment.
 */
public class LeaveEvent extends Event {
    
    /**
     * Constructor for LeaveEvent.
     * 
     * @param   date    the date that the resident leaves.  Cannot be null.
     * @param   name    the name of the resident.
     */
    public LeaveEvent(Date date, String name) {
        super(date, name);
    }


    /**
     * Sets the {@link Resident#setDepartureDate(Date) departure date} for 
     * the resident.
     * 
     * @param   manager the FinancialManager to help process this Event.  
     *                  Cannot be null.
     */
    public void process(FinanceManager manager) {
        Resident resident = manager.findResident(getName());
        if (resident == null) {
            throw new IllegalStateException("Resident " + getName()
                    + " cannot leave as he/she has never arrived.");
        }
        
        manager.residentLeaves(resident, getDate());
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
    
        super.validate(propertyName, manager);
        if (propertyName.equals("name")) {
            resident(propertyName, getName(), manager);
        }
    }
}
